#pragma once
#include "Protocol.hpp"

namespace wxd
{
    class Calculator
    {
    public:
        Calculator() {};
        Response Excute(const Request &request)
        {
            Response response;
            switch (request.Oper())
            {
            case '+':
                response.SetResult(request.X() + request.Y());
                break;
            case '-':
                response.SetResult(request.X() - request.Y());
                break;
            case '*':
                response.SetResult(request.X() * request.Y());
                break;
            case '/':
            {
                if (request.Y() == 0)
                {
                    response.SetCalCode(1);
                    break;
                }
                response.SetResult(request.X() / request.Y());
                break;
            }
            case '%':
                if (request.Y() == 0)
                {
                    response.SetCalCode(2);
                    break;
                }
                response.SetResult(request.X() % request.Y());
                break;

            default:
                response.SetCalCode(3);
                break;
            }
            return response;
        }
    }cal;

    //报头解析接口
    std::string Parse(std::string& package)
    {
        std::string resp_packet;
        std::string content;
        while(Decode(package,content))
        {
            //报头解析成功
            //反序列化
            Request request;
            request.Deserialize(content);
            //执行任务-业务处理
            Response response = cal.Excute(request);
            //序列化
            std::string response_str;
            response.Serialize(response_str);
            //添加报头
            Encode(response_str);
            //追加响应报文
            resp_packet += response_str;
        }
        return resp_packet;
    }
}